﻿
// ChildView.cpp: CChildView 类的实现
//

#include "stdafx.h"
#include "testWin1.h"
#include "ChildView.h"
#include "CMyGraph.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define ST_POINT	1   //对操作进行宏定义，即后面选择1的时候进行ST_POINT对应的操作
#define ST_LINE		2   //对操作进行宏定义，即后面选择2的时候进行ST_LINE对应的操作
#define ST_TRI      3   //对操作进行宏定义，即后面选择3的时候进行ST_TRI对应的操作
#define ST_CIRCLE	4
#define ST_CLEAR	-1  //对操作进行宏定义，即后面选择-1的时候进行ST_CLEAR对应的操作
#define ST_NONE		0   //对操作进行宏定义，即后面选择0的时候进行ST_NONE对应的操作
int MyMenuSt = 0;

CMyGraph graph;  //创建graph对象
CBox<int> box;
// CChildView


CChildView::CChildView()
{
}

CChildView::~CChildView()
{
}


BEGIN_MESSAGE_MAP(CChildView, CWnd)
	ON_WM_PAINT()
	ON_COMMAND(ID_POINT, &CChildView::OnPoint)
	ON_COMMAND(ID_LINE, &CChildView::OnLine)
	ON_COMMAND(ID_NONE, &CChildView::OnNone)
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_SAVE, &CChildView::OnSave)
	ON_COMMAND(ID_LOAD, &CChildView::OnLoad)
	ON_COMMAND(ID_CLEAR, &CChildView::OnClear)
	ON_COMMAND(ID_TRI, &CChildView::OnTri)
	ON_COMMAND(ID_CIRCLE, &CChildView::OnCircle)
END_MESSAGE_MAP()


// CChildView 消息处理程序

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
	if (!CWnd::PreCreateWindow(cs))
		return FALSE;

	cs.dwExStyle |= WS_EX_CLIENTEDGE;
	cs.style &= ~WS_BORDER;
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
		::LoadCursor(nullptr, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_WINDOW+1), nullptr);

	return TRUE;
}

CDC *myDC = NULL;
void gdiDrawLine(T_Type x0, T_Type y0, T_Type x1, T_Type y1)
{
	myDC->MoveTo(x0, y0);
	myDC->LineTo(x1, y1);
}

void gdiDrawCircle(T_Type x0, T_Type y0, double radius)
{
	int i, n, xi, yi;
	double x, y, x1, y1, ang, cosa, sina;


	n = 40;
	x = radius;
	y = 0;
	ang = PI * 2 / n;
	cosa = cos(ang);
	sina = sin(ang);
	xi = (int)(x0 + x);
	yi = (int)(y0 + y);
	myDC->MoveTo(xi, yi);
	for (i = 0; i < n; i++)
	{
		x1 = x * cosa - y * sina;
		y1 = x * sina + y * cosa;
		x = x1;
		y = y1;
		xi = (int)(x0 + x);
		yi = (int)(y0 + y);
		myDC->LineTo(xi, yi);
	}
}

void CChildView::OnPaint() 
{
	CPaintDC dc(this); // 用于绘制的设备上下文
	myDC = &dc;       //将绘制事件地址赋值给myDC指针
	// TODO: 在此处添加消息处理程序代码
	
	graph.Draw();

	CPen pen(PS_DASH, 1, RGB(255, 0, 0));
	CPen* pOldPen = dc.SelectObject(&pen);
	box.Draw();
	dc.SelectObject(pOldPen);

	/*
	// 不要为绘制消息而调用 CWnd::OnPaint()
	CBrush brush;//申明一个格式刷对象
	brush.CreateSolidBrush(RGB(255, 0, 0));//设置颜色
	dc.FillRect(CRect(100, 100, 230, 130), &brush);

	dc.DrawText(L"test中文", CRect(100, 250, 230, 330), DT_CENTER | DT_VCENTER);
	*/
}



void CChildView::OnPoint()
{
	// TODO: 在此添加命令处理程序代码
	MyMenuSt = ST_POINT;
}


void CChildView::OnLine()
{
	// TODO: 在此添加命令处理程序代码
	MyMenuSt = ST_LINE;
}


void CChildView::OnNone()
{
	// TODO: 在此添加命令处理程序代码
	MyMenuSt = ST_NONE;
}


void CChildView::OnTri()
{
	MyMenuSt = ST_TRI;

	// TODO: 在此添加命令处理程序代码
}


void CChildView::OnCircle()
{
	// TODO: 在此添加命令处理程序代码
	MyMenuSt = ST_CIRCLE;
}

//鼠标事件
void CChildView::OnLButtonDown(UINT nFlags, CPoint point)  
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	static vector<int> buf;
	int x, y;
	int ptbuf[2];


	switch (MyMenuSt)//选择画点、线还是三角形
	{
	case ST_POINT:
		CMyPoint *pt;
		pt = new CMyPoint();
		pt->SetPoint(point.x, point.y);

		ptbuf[0] = point.x;
		ptbuf[1] = point.y;
		box.AddPoint(ptbuf);

		graph.InsertObj(pt);
		Invalidate();
		break;
	case ST_LINE:
		if (buf.size() > 0)
		{
			x = buf[0];
			y = buf[1];
			CMyLine *pt;
			pt = new CMyLine();
			pt->SetPoint(0, x, y);
			pt->SetPoint(1, point.x, point.y);
			graph.InsertObj(pt);
			buf.clear();
			Invalidate();
		}
		else
		{
			buf.push_back(point.x);
			buf.push_back(point.y);
		}
		break;
	case ST_TRI:
		if (buf.size() >= 4 )
		{
			CMyTri *pt;
			pt = new CMyTri();
			x = buf[0];
			y = buf[1];
			pt->SetPoint(0, x, y);
			x = buf[2];
			y = buf[3];
			pt->SetPoint(1, x, y);
			pt->SetPoint(2, point.x, point.y);
			graph.InsertObj(pt);
			buf.clear();
			Invalidate();
		}
		else
		{
			buf.push_back(point.x);
			buf.push_back(point.y);
		}
		break;
	case ST_CIRCLE:
		if (buf.size() > 0)
		{
			x = buf[0];
			y = buf[1];
			CMyCircle *pt;
			pt = new CMyCircle();
			double dx, dy, r;
			dx = point.x - x;
			dy = point.y - y;
			r = sqrt(dx*dx + dy*dy);
			pt->SetCircle(x, y, r);
			graph.InsertObj(pt);
			buf.clear();
			Invalidate();
		}
		else
		{
			buf.push_back(point.x);
			buf.push_back(point.y);
		}
		break;
	case ST_NONE:  
		buf.clear();
		break;
	}

	CWnd::OnLButtonDown(nFlags, point);
}


void CChildView::OnSave()
{
	CString filter = L"文本文档(*.txt)|*.txt||";
	CFileDialog dlg(FALSE, NULL, NULL, 0, filter);
	if (dlg.DoModal() != IDOK)
		return;

	fstream out(dlg.GetPathName(), ios::out);
	graph.Save(out);
	out.close();
}


void CChildView::OnLoad()
{
	CString filter = L"文本文档(*.txt)|*.txt||";
	CFileDialog dlg(TRUE, NULL, NULL, 0, filter);
	if (dlg.DoModal() != IDOK)
		return;

	fstream in(dlg.GetPathName(), ios::in);
	graph.Load(in);
	in.close();
	Invalidate();
}


void CChildView::OnClear()
{
	graph.Clear();
	Invalidate();
}


